package xyz.thoughtset.viewer.common.ai.model.service;

import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JavaType;
import jakarta.annotation.PostConstruct;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import xyz.thoughtset.viewer.common.ai.model.dao.AiNodeDao;
import xyz.thoughtset.viewer.common.ai.model.entity.AiNode;
import xyz.thoughtset.viewer.common.crud.core.service.BaseServiceImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AiNodeServiceImpl extends BaseServiceImpl<AiNodeDao, AiNode> implements AiNodeService {
    @SneakyThrows
    @Override
    public AiNode saveData(AiNode data) {
        if (!ObjectUtils.isEmpty(data.getSettingMap())){
            data.setSettingStr(mapper.writeValueAsString(data.getSettingMap()));
        }
        if (!ObjectUtils.isEmpty(data.getHeaderMap())){
            data.setHeadersStr(mapper.writeValueAsString(data.getHeaderMap()));
        }
        return super.saveData(data);
    }

    @Override
    @SneakyThrows
    public AiNode selectDetail(String pkey) {
        AiNode data = super.selectDetail(pkey);
        if (StringUtils.hasText(data.getSettingStr())){
            data.setSettingMap(mapper.readValue(data.getSettingStr(), Map.class));
        }
        if (StringUtils.hasText(data.getHeadersStr())){
            data.setHeaderMap(mapper.readValue(data.getHeadersStr(), Map.class));
        }
        return data;
    }
}
